package com.zhijian.warehouse.manage.config;

import com.zhijian.warehouse.manage.enums.SystemEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * swagger配置类
 *
 * @author HCG
 * @version 1.0
 * @date 2021/1/26
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * 添加摘要信息
     */
    @Bean
    public Docket controllerApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("接口文档")
                        .description("描述：Swagger接口文档")
                        .version("版本号:1.0")
                        .build())
                .select()
                //controller层的包地址
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                //token 属性
                .securitySchemes(securitySchemes())
                //token 值
                .securityContexts(securityContexts());
    }

    private List<ApiKey> securitySchemes() {
        return Arrays.asList(new ApiKey(SystemEnum.WEB.getTokenKey(), SystemEnum.WEB.getTokenKey(), "header"),
                new ApiKey(SystemEnum.MP.getTokenKey(), SystemEnum.MP.getTokenKey(), "header"));
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference(SystemEnum.WEB.getTokenKey(), authorizationScopes));
        securityReferences.add(new SecurityReference(SystemEnum.MP.getTokenKey(), authorizationScopes));
        return securityReferences;
    }


}

